package com.egao.common.manage;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.egao.common.module.system.entity.College;
import com.egao.common.module.system.entity.Dict;
import com.egao.common.module.system.entity.User;
import com.egao.common.module.system.mapper.CollegeMapper;
import com.egao.common.module.system.mapper.DictMapper;
import com.egao.common.module.system.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommonRefService {
    private final UserMapper userMapper;
    private final CollegeMapper collegeMapper;
    private final DictMapper dictMapper;

    @Autowired
    public CommonRefService(UserMapper userMapper, CollegeMapper collegeMapper, DictMapper dictMapper) {
        this.userMapper = userMapper;
        this.collegeMapper = collegeMapper;
        this.dictMapper = dictMapper;
    }

    /**
     * 用户引用
     */
    public Map<Integer, User> refUser(List<Integer> ids) {
        Map<Integer, User> map = new HashMap<Integer, User>();
        if (ids != null && ids.size() > 0) {
            List<User> models = userMapper.selectBatchIds(ids);
            for (User model : models) {
                map.put(model.getUserId(), model);
            }
        }
        return map;
    }

    /**
     * 学院引用
     */
    public Map<String, College> refCollege(List<String> codes) {
        Map<String, College> map = new HashMap<String, College>();
        if (codes != null && codes.size() > 0) {
            QueryWrapper<College> wrapper = new QueryWrapper<>();
            wrapper.in("code", codes);
            List<College> models = collegeMapper.selectList(wrapper);
            for (College model : models) {
                map.put(model.getCode(), model);
            }
        }
        return map;
    }

    /**
     * 字典引用
     */
    public Map<String, Dict> refDict(List<String> vals, String type) {
        Map<String, Dict> map = new HashMap<String, Dict>();
        if (vals != null && vals.size() > 0 && StrUtil.isNotBlank(type)) {
            QueryWrapper<Dict> wrapper = new QueryWrapper<>();
            wrapper.eq("type", type);
            wrapper.in("value", vals);
            List<Dict> models = dictMapper.selectList(wrapper);
            for (Dict model : models) {
                map.put(model.getValue(), model);
            }
        }
        return map;
    }

}
